﻿/*
 * Mr.Chau
 * 27/5/2013
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucNhapThietBiDienTuFileExcel : UserControl
    {
        #region "[khai báo biến tòan cục]"
      
        #endregion

        #region "[Khai báo delegate]"
        /// <summary>
        /// Khai báo delegate chuyển đến usercontrol nhập thiết bị điện thủ công
        /// </summary>
        public DelegateShowImportExcel showItem = null;
        public DelegateShowItem ClearItem = null;
        #endregion

        #region "[Load]"
        public ucNhapThietBiDienTuFileExcel()
        {
            InitializeComponent();
        }
        #endregion

        #region "[Hiển thị dữ liệu trong file excel ra datagridview]"
        private void btnHienThi_Click(object sender, EventArgs e)
        {
            try { 
                //lấy đường dẫn đến file excel
                if (txtDuongDan.Text.Trim() == string.Empty)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Chưa chọn tên đường dẫn đến excel", "Thông Báo", true);
                    return;
                }

                //
                 List<ThietBiDienDTO> list = NangLuongTieuDiem_Management.ThietBiDien_GetListFromFileExcel(txtDuongDan.Text.Trim(), (int)numericUpDownSheet.Value);
                dgvThietBiDien.DataSource = list;
                dgvThietBiDien.RefreshDataSource();
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("btnHienThi_Click", ex);
            }
        }
        #endregion

        #region "[Nhập thiết bị điện bằng phương pháp thủ công]"
        private void btnNhapThuCong_Click(object sender, EventArgs e)
        {
            try {
                if (showItem != null)
                    showItem();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnNhapThuCong_Click", ex);
            }
        }
        #endregion

        #region "[chức năng thoát]"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            try {
                if (ClearItem != null)
                    ClearItem(false);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnThoat_Click", ex);
            }
        }
        #endregion

        #region "[xóa dữ liệu]"
        private void btnXoaDuLieu_Click(object sender, EventArgs e)
        {
            try {
                txtDuongDan.Text = string.Empty;
                dgvThietBiDien.DataSource = null;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnXoaDuLieu_Click", ex);
            }
        }
        #endregion

        #region "[Lấy đường dẫn đến file excel]"
        private void txtDuongDan_DoubleClick(object sender, EventArgs e)
        {
            try {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "File Excel|*.xls";
                if (open.ShowDialog() == DialogResult.OK)
                    txtDuongDan.Text = open.FileName;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("txtDuongDan_DoubleClick", ex);
            }
        }
        #endregion

        #region "[Nhập danh sách các thiết bị vào CSDL]"
        private void btnNhap_Click(object sender, EventArgs e)
        {
            try {
                if (txtDuongDan.Text.Trim() == string.Empty)
                { 
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Chưa chọn đường dẫn đến file excel","Thông Báo",true);
                    return;
                }
                if (numericUpDownSheet.Value < 1)
                {
                    ClassUtils.Utils.MassageBoxErrorOtInformation("Sheet không được nhỏ hơn 1", "Thông Báo", true);
                    return;
                }
                
                //nhập thông tin thiết bị
                List<ThietBiDienDTO> list = (List<ThietBiDienDTO>)dgvThietBiDien.DataSource;
                if (list.Count > 0)
                {
                    NangLuongTieuDiemBUS.ACK.SanPham_ThietBiDienACK ItemResult = NangLuongTieuDiem_Management.ThietBiDien_InsertListThietBiDien(list);
                    if (ItemResult.Error)
                    {
                        //có lỗi trong quá trình insert.
                        string strError = string.Empty;
                        foreach (int i in ItemResult.RowID)
                        {
                            strError += "Không Insert được dòng thứ :" + (i + 1).ToString() + "\r\n";
                        }
                        ClassUtils.Utils.MassageBoxErrorOtInformation(strError, "Thông Báo", true);
                    }
                    else
                        ClassUtils.Utils.MassageBoxErrorOtInformation("Thêm danh sách các thiết bị thành công!", "Thông Báo", false);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnNhap_Click", ex);
            }
        }
        #endregion

        #region "[Đánh số thứ tự]"
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try {
                if (e.Column.Name == "colSTT")
                    e.DisplayText = (e.RowHandle + 1).ToString();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("gridView1_CustomDrawCell", ex);
            }
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi ="ucNhapThietBiDienTuFileExcel_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion
    }
}
